<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avater_box">
        <img src="../assets/用户登录.png" alt="">
      </div>
      <!--表单区域 -->
      <el-form label-width="0px" ref="formRef" class="login_form" :model="loginForm" :rules="rules">
        <!-- 用户名 -->
      <el-form-item prop="username">
      <el-input prefix-icon="iconfont icon-user" v-model.trim="loginForm.username"></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
      <el-input prefix-icon="iconfont icon-3702mima" v-model.trim="loginForm.password" type="password"></el-input>
      </el-form-item>
      <!-- 按钮区域 -->
      <el-form-item class="btns">
      <el-button type="primary" @click="commitForm">提交</el-button>
      <el-button type="primary" @click="resetForm">重置</el-button>
      </el-form-item>
      </el-form >
    </div>
  </div>
</template>

<script>
export default {
    name:'login',
    data() {
      return {
        //登录表单信息绑定的对象
        loginForm:{
           username:'',
           password:''
        },
        //表单的验证规则对象
        rules:{
        // 验证用户名是否合法
         username:[
            { required: true, message: '请输入登录名称', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字', trigger: 'blur' }
          ],
        // 验证密码是否合法
        password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在  6到 20 位密码', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      resetForm(){
        // console.log(this);这里的this是login组件实例对象
        this.$refs.formRef.resetFields();
      },
      commitForm(){
        this.$refs.formRef.validate(async valid=>{
           if(!valid) return;this.loginForm
          //  console.log(this.loginForm);
          const {data,}= await this.$http.post('login',this.loginForm);
          // console.log(data);
          if(data.meta.status!==200)return this.$message.error('登录失败')
          // console.log(data);
          //保存返回的token到本地
          window.sessionStorage.setItem('token',data.data.token);
          this.$message.success('登录成功');
          this.$router.push("/home")
        })
      }
    }
}
</script>

<style lang="less" scoped>
.login_container{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  background-color: #74787c;
  .login_box{
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #ffffff;
    border: 1px solid #7d5886;
    border-radius: 20px;
    .avater_box{
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      height: 100px;
      width: 100px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      background-color: #eee;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
  }
}
.login_form{
  position: absolute;
  top: 40%;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
.btns{
  display: flex;
  justify-content: center;
}
</style>
